package com.hrms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hrms.dao.*;
import com.hrms.pojo.*;
import com.hrms.service.PersonnelTransferService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 人事调动表 服务实现类
 * </p>
 *
 * @author LK
 * @since 2021-10-19
 */
@Service
public class PersonnelTransferServiceImpl  implements PersonnelTransferService {
    @Autowired(required = false)
    PersonnelTransferMapper personnelTransferMapper;
    @Autowired(required = false)
    SysDeptMapper sysDeptMapper;
    @Autowired(required = false)
    SysRoleMapper sysRoleMapper;
    @Autowired(required = false)
    EmpInformationMapper empInformationMapper;
    @Autowired(required = false)
    UserRoleRefMapper userRoleRefMapper;


    @Override
    public HashMap<String, Object> select(PersonnelTransfer p) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //定义分页对象
        Page<PersonnelTransfer> page = new Page<>(p.getPage(),p.getRow());

        //查询部门
        List<SysDept> sysDepts = sysDeptMapper.selectList(null);
        map.put("depts",sysDepts);


        /*-------------mybatit plus的分页条件查询 -------------- */
        //查询分页
        QueryWrapper<PersonnelTransfer> queryWrapper = new QueryWrapper<>();
        //编号查询
        queryWrapper.eq(p.getStaffId()!=null,"staff_id",p.getStaffId());
        //按照时间排序
        queryWrapper.orderByAsc(p.getOrder()==0,"chenge_time");
        queryWrapper.orderByDesc(p.getOrder()==1,"chenge_time");
        //用户名模糊查询
        queryWrapper.like(!StringUtils.isEmpty(p.getStaffName())&&p.getStaffName()!=null,"staff_name",p.getStaffName());
        /*//时间排序
        queryWrapper.orderByDesc("chenge_time");*/
        Page<PersonnelTransfer> personnelTransferPage = personnelTransferMapper.selectPage(page, queryWrapper);
        map.put("list",personnelTransferPage.getRecords());
        //总页数
        map.put("totalPage",personnelTransferPage.getPages());
        //总条数
        map.put("total",personnelTransferPage.getTotal());
        //当前页
        map.put("curPage",p.getPage());
        //上一页
        map.put("prePage",p.getPage()-1);
        //下一页
        map.put("nextPage",p.getPage()+1);


        return map;
    }

    @Override
    public HashMap<String, Object> add(PersonnelTransfer p) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int insert = personnelTransferMapper.insert(p);
        if (insert==0){
            map.put("info","保存失败");
        }else {
            map.put("info","保存成功");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(PersonnelTransfer p) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int update = personnelTransferMapper.updateById(p);
        if (update==0){
            map.put("info","保存失败");
        }else {
            map.put("info","保存成功");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> delete(PersonnelTransfer p) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int delete = personnelTransferMapper.deleteById(p.getId());
        if (delete==0){
            map.put("info","删除失败");
        }else {
            map.put("info","删除成功");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> getRoleListByDept(String p) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<String> list = personnelTransferMapper.selectRoleListByDept(p);
        map.put("roles",list);
        return map;
    }

    @Override
    public HashMap<String, Object> getEmpInformation(EmpInformation e) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //定义分页对象
        Page<EmpInformation> page = new Page<>(e.getPage(),e.getRow());

        //查询部门
        List<SysDept> sysDepts = sysDeptMapper.selectList(null);
        map.put("depts",sysDepts);


        /*-------------mybatit plus的分页条件查询 -------------- */
        //查询分页
        QueryWrapper<EmpInformation> queryWrapper = new QueryWrapper<>();
        //编号查询
        queryWrapper.eq(e.getStaffId()!=null,"staff_id",e.getStaffId());
        //按照时间排序
        queryWrapper.orderByAsc(e.getOrder()==0,"staff_entry_time");
        queryWrapper.orderByDesc(e.getOrder()==1,"staff_entry_time");
        //用户名模糊查询
        queryWrapper.like(!StringUtils.isEmpty(e.getStaffName())&&e.getStaffName()!=null,"staff_name",e.getStaffName());

        Page<EmpInformation> personnelTransferPage = empInformationMapper.selectPage(page, queryWrapper);
        map.put("list",personnelTransferPage.getRecords());
        //总页数
        map.put("totalPage",personnelTransferPage.getPages());
        //总条数
        map.put("total",personnelTransferPage.getTotal());
        //当前页
        map.put("curPage",e.getPage());
        //上一页
        map.put("prePage",e.getPage()-1);
        //下一页
        map.put("nextPage",e.getPage()+1);


        return map;
    }

    @Override
    public HashMap<String, Object> updateEmpInformation(EmpInformation e) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int update = empInformationMapper.updateById(e);
        if (update==0){
            map.put("info","保存失败");
        }else {
            map.put("info","保存成功");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> updateEmpInformationByStaffId(EmpInformation e) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int update = empInformationMapper.updateEmpInformationByStaffId(e);
        if (update==0){
            map.put("info","保存失败");
        }else {
            map.put("info","保存成功");
        }
        return map;
    }

    @Override
    public EmpInformation getEmpInformationOne(EmpInformation e) {
        return empInformationMapper.selectById(e.getId());
    }

    @Override
    public void upSysDeptNumber(String oldDept, String newDept) {
        sysDeptMapper.updateDeptNumberAdd(oldDept);
        sysDeptMapper.updateDeptNumberRed(newDept);
    }

    @Override
    public PersonnelTransfer selectOne(Integer id) {
        return personnelTransferMapper.selectById(id);
    }

    @Override
    public UserRoleRef selectUserRoleRef(int staffId) {
        return personnelTransferMapper.selectUserIdAndRoleId(staffId);
    }

    @Override
    public int selectRoleIdByRoleName(String roleName) {
        return sysRoleMapper.selectRoleIdByRoleName(roleName);
    }

    @Override
    public int updateUserRole(UserRoleRef userRoleRef) {
        return userRoleRefMapper.updateById(userRoleRef);
    }


}
